package myta.bean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.model.SelectItem;

import myta.base.FormDataBase;
import myta.model.DataRow;
import myta.model.DataTable;
import myta.service.Command;
import myta.service.SQLexec;
import myta.service.Utility;

public class AbsenData extends FormDataBase{
	private HtmlInputText txtid;
	private HtmlInputText txttanggal;
	private HtmlSelectOneMenu txtiskelassiswa;
	private HtmlSelectOneMenu txtidmapel;
	
	private List<SelectItem> listKelasSiswa;
	private List<SelectItem> listMapel;
	
	private DataDetail dd;
	
	@Override
	public void init() throws Exception {
		formName = "absen";
		dd = (DataDetail)Utility.getBean("dataDetail");
		
		txtid = new HtmlInputText();
		txttanggal = new HtmlInputText();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		txttanggal.setValue(sdf.format(Calendar.getInstance().getTime()));
		
		loadList();
	}
	
	private void loadList() throws Exception{
		listKelasSiswa = new ArrayList<SelectItem>();
		listMapel = new ArrayList<SelectItem>();
		
		SelectItem s = new SelectItem();
		s.setValue("-");
		s.setLabel("-");
		
		listKelasSiswa.add(s);
		listMapel.add(s);
		
		String sql = "select ks.idkelassiswa, concat(k.namakelas,' ~ ',s.thnajaran) as namakelas from kelassiswa ks inner join kelas k on ks.idkelas = k.idkelas inner join semester s on k.idsemester = s.idsemester";
		Command command = SQLexec.makeCommand(sql);
		DataTable dt = command.executeQuery();
		
		for(int i=0;i<dt.getRowCount();i++){
			DataRow dr = dt.getRow(i);
			
			s = new SelectItem();
			s.setValue(dr.getString("idkelassiswa"));
			s.setLabel(dr.getString("namakelas"));
			
			listKelasSiswa.add(s);
		}
		
		sql = "select idmapel,namamapel from mapel";
		command = SQLexec.makeCommand(sql);
		dt = command.executeQuery();
		
		for(int i=0;i<dt.getRowCount();i++){
			DataRow dr = dt.getRow(i);
			
			s = new SelectItem();
			s.setValue(dr.getString("idmapel"));
			s.setLabel(dr.getString("namamapel"));
			
			listMapel.add(s);
		}
	}
		
	public void loadDataSiswa(){
		try {
			String idkelassiswa = txtiskelassiswa.getValue().toString();
			
			String sql = "select concat('') as idabsen, s.nis, s.nama, concat('true') as absensi from kelassiswa ks ";
			sql += "inner join kelassiswa_detail ksd on ks.idkelassiswa = ksd.idkelassiswa ";
			sql += "inner join siswa s on ksd.nis = s.nis ";
			sql += "where ks.idkelassiswa = ?";
			Command command =SQLexec.makeCommand(sql);
			command.setObject(1, idkelassiswa);
			DataTable dt = command.executeQuery();
			dd.getDataAbsensi().removeAll();
			
			for(int i=0;i<dt.getRowCount();i++){
				dd.getDataAbsensi().addRow(dt.getRow(i));
			}
		} catch (Exception e) {
			Utility.showMessage(e.getMessage());
		}
	}
	
	@Override
	public void generateID() throws Exception {
		String sql = "call idabsen()";
		Command command = SQLexec.makeCommand(sql);
		DataTable datatable = command.executeQuery();
		DataRow datarow = datatable.getRow(0);
		txtid.setValue(datarow.getString("id"));
	}

	@Override
	public void getRequestData(Map<String, String> map) throws Exception {
		if(map.containsKey("idabsensi")){
			String idabsensi = map.get("idabsensi");
			
			String sql = "select * from absen where idabsen = ?";
			
			Command command = SQLexec.makeCommand(sql);
			command.setObject(1, idabsensi);
			DataTable datatable = command.executeQuery();
			DataRow datarow = datatable.getRow(0);
			
			txtid.setValue(datarow.getString("idabsen"));
			txttanggal.setValue(datarow.getString("tanggal"));
			txtiskelassiswa.setValue(datarow.getString("idkelassiswa"));
			txtidmapel.setValue(datarow.getString("idmapel"));
			
			sql = "select ad.idabsen, ad.nis, s.nama, case when ad.absensi = 0 then false else true end as absensi from absen_detail ad ";
			sql += "inner join siswa s on ad.nis = s.nis ";
			sql += "where ad.idabsen = ? ";
			sql += "order by s.nama";
			command = SQLexec.makeCommand(sql);
			command.setObject(1, idabsensi);
			DataTable dt = command.executeQuery();
			
			for(int i=0;i<dt.getRowCount();i++){
				dd.getDataAbsensi().addRow(dt.getRow(i));
			}
		}
	}

	@Override
	public boolean simpan() throws Exception {
		boolean boo = false;
		
		String sql = "INSERT INTO absen(idabsen,tanggal,idkelassiswa,idmapel) VALUES(?,?,?,?);";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtid.getValue().toString());
		command.setObject(2, txttanggal.getValue().toString());
		command.setObject(3, txtiskelassiswa.getValue().toString());
		command.setObject(4, txtidmapel.getValue().toString());
		boo = command.executeUpdate();
		
		for(int i=0;i<dd.getDataAbsensi().getRowCount();i++){
			DataRow dr = dd.getDataAbsensi().getRow(i);
			String idabsen = txtid.getValue().toString();
			String nis = dr.getString("nis");
			Integer absensi = Boolean.valueOf(dr.getString("absensi"))?1:0;
			
			sql = "insert into absen_detail(idabsen,nis,absensi) values(?,?,?)";
			command = SQLexec.makeCommand(sql);
			command.setObject(1, idabsen);
			command.setObject(2, nis);
			command.setObject(3, absensi);
			boo = command.executeUpdate();
		}
		
		if(boo){
			return true;
		}else{
			return false;
		}		
	}

	@Override
	public boolean ubah() throws Exception {
		boolean boo = false;
		
		String sql = "UPDATE absensi SET tanggal=?,idkelassiswa=?,idmapel=? where idabsen = ?;";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txttanggal.getValue().toString());
		command.setObject(2, txtiskelassiswa.getValue().toString());
		command.setObject(3, txtidmapel.getValue().toString());
		command.setObject(4, txtid.getValue().toString());
		boo = command.executeUpdate();
		
		for(int i=0;i<dd.getDataAbsensi().getRowCount();i++){
			DataRow dr = dd.getDataAbsensi().getRow(i);
			String idabsen = dr.getString("idabsen");
			String nis = dr.getString("nis");
			Integer absensi = Boolean.valueOf(dr.getString("absensi"))?1:0;
			
			sql = "update absen_detail set nis=?,absensi=? where idabsen=?";
			command = SQLexec.makeCommand(sql);
			command.setObject(1, nis);
			command.setObject(2, absensi);
			command.setObject(3, idabsen);
			boo = command.executeUpdate();
		}
		
		if(boo){
			return true;
		}else{
			return false;
		}
	}
	
	@Override
	public boolean beforeCancel() {
		dd.getDataAbsensi().removeAll();
		return true;
	}

	public HtmlInputText getTxtid() {
		return txtid;
	}

	public void setTxtid(HtmlInputText txtid) {
		this.txtid = txtid;
	}
	
	public HtmlInputText getTxttanggal() {
		return txttanggal;
	}

	public void setTxttanggal(HtmlInputText txttanggal) {
		this.txttanggal = txttanggal;
	}

	public HtmlSelectOneMenu getTxtiskelassiswa() {
		return txtiskelassiswa;
	}

	public void setTxtiskelassiswa(HtmlSelectOneMenu txtiskelassiswa) {
		this.txtiskelassiswa = txtiskelassiswa;
	}

	public HtmlSelectOneMenu getTxtidmapel() {
		return txtidmapel;
	}

	public void setTxtidmapel(HtmlSelectOneMenu txtidmapel) {
		this.txtidmapel = txtidmapel;
	}

	public List<SelectItem> getListKelasSiswa() {
		return listKelasSiswa;
	}

	public void setListKelasSiswa(List<SelectItem> listKelasSiswa) {
		this.listKelasSiswa = listKelasSiswa;
	}

	public List<SelectItem> getListMapel() {
		return listMapel;
	}

	public void setListMapel(List<SelectItem> listMapel) {
		this.listMapel = listMapel;
	}
}
